import numpy as np
from tabulate import tabulate

"""
1. 使用 ANSI 转义码（适用于命令行环境）

ANSI 转义码是一种控制字符序列，可以控制终端的文本显示方式，包括颜色等属性。

基本的颜色代码：
\033[0m：重置所有属性
\033[30m - \033[37m：设置文本颜色（30 - 黑色，31 - 红色，32 - 绿色，33 - 黄色，34 - 蓝色，35 - 紫色，36 - 青绿色，37 - 白色）
\033[40m - \033[47m：设置文本背景颜色
"""

table = np.zeros((41, 41), dtype=np.int32)


# data = [["1", "2"], ["3", "4"]]
#
# data = table
# print(tabulate(data, tablefmt="grid"))

matrix = table

# formatted_matrix = [[ '' if element == 0 else element for element in row] for row in matrix]
# print(tabulate(formatted_matrix, tablefmt="grid"))


def handle(i: int) -> str:
    if i == 0:
        return ""
    elif i == 1:
        return f"\033[31m{i:02d}\033[0m" # red
    elif i == 2:
        return f"\033[34m{i:02d}\033[0m" # blue
    elif i == 3:
        return f"\033[41m{i:02d}\033[0m" # blue
    else:
        return f"{i:02d}"


def red(s: str) -> str:
    return f"\033[31m{s}\033[0m"


# 0 -> red
colored_matrix = [[red(ele) if ele == 0 else ele for ele in row] for row in matrix]


matrix[3][9] = 1
matrix[4][7] = 2
matrix[5][6] = 3

# 1 -> red
colored_matrix = [[handle(ele) for ele in row] for row in matrix]
print(tabulate(colored_matrix, tablefmt="grid"))
